package cn.tom.controller.adm;

import cn.tom.entity.Type;
import cn.tom.service.BookTypeService;
import cn.tom.tool.XJson;
import cn.tom.tool.XResp;
import java.time.LocalDate;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping({"/api/adm/type"})
@RestController
public class TypeController {
    private Logger log = LoggerFactory.getLogger(getClass());
    @Autowired
    private BookTypeService bookTypeService;



    @GetMapping({"/findAll"})
    public XJson findAllType() {
        return XResp.pack(200, "Ok", this.bookTypeService.findAll());
    }

    @GetMapping({"/findAll2"})
    public XJson findAllType2() {
        List<Type> lst = this.bookTypeService.findAll2();
        return XResp.pack(200, "Ok", lst);
    }

    @PostMapping({"/add"})
    public XJson addype(Integer id, String name, Integer pid, Integer prio) {
        if (pid == null) {
            pid = 0;
        }
        if (prio == null) {
            prio = 1;
        }
        if (id == null || id == 0) {
            this.bookTypeService.add(name, pid, prio);
        }
        if (id > 0) {
            this.bookTypeService.update(id, name, pid, prio);
        }
        return XResp.pack(200, "添加成功", this.bookTypeService.findAll2());
    }

    @GetMapping({"/findById/{id}"})
    public XJson findTypeById(@PathVariable Integer id) {
        this.log.info("更新请求 id=" + id);
        return XResp.pack(200, "", this.bookTypeService.findById(id));
    }

    @GetMapping({"/remove/{id}"})
    public XJson remove(@PathVariable Integer id) {
        this.log.info("删除 type  id=" + id);
        this.bookTypeService.remove(id);
        return XResp.pack(200, "删除成功", null);
    }
}
